<template>
  <el-container>
    <el-header>
      <div>
        <img src="../assets/cat.jpg" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click='logout'>退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="iscollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <el-menu
              background-color="#333744"
              text-color="#fff"
              active-text-color="#409eff"
              unique-opened
              :collapse="iscollapse"
              :collapse-transition="false"
              router
              :default-active="activepath">
              <el-submenu :index="item.id + ''" v-for="item in menulist" :key='item.id'>
                <template slot="title">
                  <i :class="icons[item.id]"></i>
                  <span>{{ item.authName }}</span>
                </template>
                  <el-menu-item :index="'/' + subitem.path" v-for="subitem in item.children" :key="subitem.id" @click="saveNavState('/' + subitem.path)">
                    <template slot="title">
                      <i class="el-icon-menu"></i>
                      <span>{{ subitem.authName }}</span>
                    </template>
                  </el-menu-item>
              </el-submenu>
            </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'home',
  data () {
    return {
      menulist: [],
      icons: {
        125: 'iconfont icon-qunzu',
        103: 'iconfont icon-icon-adjustment',
        101: 'iconfont icon-chukuguanli',
        102: 'iconfont icon-icon-truck',
        145: 'iconfont icon-icon-trend'
      },
      iscollapse: false,
      activepath: ''
    }
  },
  created () {
    this.getMenuList()
    this.activepath = window.sessionStorage.getItem('activepath')
  },
  methods: {
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menulist = res.data
      console.log(res)
    },
    toggleCollapse () {
      this.iscollapse = !this.iscollapse
    },
    saveNavState (activePath) {
      window.sessionStorage.setItem('activepath', activePath)
      this.activepath = activePath
    }
  }
}
</script>

<style>
  .el-container{
    height: 100%;
  }
  .el-header{
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: #fff;
    font-size: 20px;
  }
  .el-header div{
    display: flex;
    align-items: center;
  }
  .el-header div span{
    padding-left: 15px;
  }
  .el-header img{
    width: 50px;
    height: 50px;
    border-radius: 50%;
  }
  .el-aside{
    background-color: #333744;
  }
  .el-main{
    background-color: #eaedf1;
  }
  .iconfont{
    margin-right: 10px;
  }
  .el-aside .el-menu{
    border-right: none;
  }
  .toggle-button{
    background-color: #4a5064;
    font-size: 10px;
    line-height: 24px;
    color: #fff;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;
  }
</style>
